﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.classes
{
    public partial class Edit : TeacherBasePage
    {
        public string act;
        public Dictionary<int, string> certificateList;
        public IList<DictAreaModel> areaList;
        public int id;
        public ClassModel model = new ClassModel();

        protected override void ShowPage()
        {
            act = XRequest.GetString("act");
            id = XRequest.GetInt("id", -1);

            ClassBLL bll = new ClassBLL();

            if (act == "u")
            {
                if (id != -1)
                {
                    model = bll.GetClassInfo(id);
                    if (model.ID == -1)
                    {
                        Alert("参数错误");
                        return;
                    }
                }
                else
                {
                    Alert("参数错误");
                    return;
                }
            }
            
            if (ispost)
            {
                int maxcount = XRequest.GetInt("MaxCountTxt", -1);

                if (CheckValue())
                {

                    model = new ClassModel();
                    model.MaxCount = maxcount;

                    int rst = 0;
                    switch (act)
                    {
                        case "n":
                            int relationid = XRequest.GetInt("CourseSlt", -1);
                            int areaid = XRequest.GetInt("AreaSlt", -1);
                            int certificateID = XRequest.GetInt("CertificateSlt", -1);
                            string classcode = XRequest.GetString("ClassCodeTxt");

                            model.TeacherID = teacherid;
                            model.ClassCode = classcode;
                            model.CertificateID = certificateID;
                            model.CourseRelationID = relationid;
                            model.AreaID = areaid;
                            model.StartTime = Convert.ToDateTime(XRequest.GetString("StartTimeTxt"));
                            model.EndTime = Convert.ToDateTime(XRequest.GetString("EndTimeTxt"));
                            model.SignUpType = XRequest.GetInt("radSignUpType", 0);
                            model.StudentArrayId = XRequest.GetString("studentIdArray");

                            rst = bll.AddClass(model);
                            break;
                        case "u":
                            model.ID = id;

                            rst = bll.UpdateClass(model);

                            break;
                        default:
                            break;
                    }

                    if (rst > 0)
                    {
                        Alert("操作成功");
                        if (act == "u")
                        {
                            ExecuteJS("window.location = '/classes/Edit.aspx?act=u&id=" + id + "';");
                        }
                        else
                            ExecuteJS("window.location = '/classes/Edit.aspx?act=n';");
                    }
                    else
                    {
                        Alert("操作失败");
                    }
                }
                else
                {
                    ExecuteJS("history.back();");
                }
            }
            if (act == "n")
            {
                areaList = GetTeacherAreaList(teacherid);
                if (authority == E.Enum.EnumTeacherAuthority.Internals)
                {
                    certificateList = (new TeacherBLL()).GetTeacherProjectInfo(teacherid);
                }
                else
                {
                    certificateList = (new CourseBLL()).GetAllCertificateAndProjectList();
                }
            }

        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("act") == "n" && XRequest.GetString("ClassCodeTxt").Equals(""))
            {
                Alert("班级编号不能为空");
                rst = false;
            }

            if (XRequest.GetInt("MaxCountTxt", 0) <= 0)
            {
                Alert("最多人数错误或不能大于30人");
                rst = false;
            }

            if (XRequest.GetString("act") == "n" && XRequest.GetString("StartTimeTxt").Equals(""))
            {
                Alert("开班时间不能为空");
                rst = false;
            }

            if (XRequest.GetString("act") == "n" && XRequest.GetString("EndTimeTxt").Equals(""))
            {
                Alert("结束时间不能为空");
                rst = false;
            }

            if (XRequest.GetString("act") == "n" && XRequest.GetInt("AreaSlt", -1) == -1)
            {
                Alert("请选择培训机构");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetInt("CourseSlt", -1) == -1)
            {
                Alert("请选择课程");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetInt("CertificateSlt", -1) == -1)
            {
                Alert("请选择证书类别");
                rst = false;
            }

            try
            {
                DateTime start = Convert.ToDateTime(XRequest.GetString("StartTimeTxt"));
                DateTime end = Convert.ToDateTime(XRequest.GetString("EndTimeTxt"));

                if (start > end)
                {
                    Alert("开班时间不能大于结束时间");
                    rst = false;
                }
            }
            catch (Exception ex)
            {
                Alert("时间格式错误");
                rst = false;
            }

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
